//
//  PYWLaunchTool.m
//  PYWGetColor
//
//  Created by 袁何涛 on 2018/7/10.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "PYWLaunchTool.h"
#import "JPUSHService.h"

@implementation PYWLaunchTool

- (void)startRunning{
    

    [self WZXLaunchView];
    
}


/**
 获取广告
 */
- (void)WZXLaunchView{
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=start" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            NSString *imageURL = responseObject[@"data"][@"imgurl"];
            NSString *timeStr = responseObject[@"data"][@"duration"];
            [[NSUserDefaults standardUserDefaults] setObject:imageURL forKey:kADImgurl];
            [UserModel sharedInstanced].advertTimeInt = [timeStr integerValue];
//            NSString *locationImgURL = [UserModel sharedInstanced].advertImg;
//            NSString *timeStr = responseObject[@"data"][@"duration"];
//            if (![imageURL isEqualToString:locationImgURL]) {
//                [UserModel sharedInstanced].advertImg = imageURL;
//                [UserModel sharedInstanced].advertTimeInt = [timeStr integerValue];
//            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求广告页失败");
    }];
}
/**
 极光别名 上传给服务器别名
 */
- (void)jspushRegisterToServers:(NSString *)equipStr{
    NSDictionary *parameter = @{@"uid":[UserModel sharedInstanced
                                  ].userId,@"equip":equipStr};
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=updateEquip" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败");
    }];
}
- (void) postServersObjectName:(NSString *)registrationID
{
    if ([UserModel sharedInstanced].userId == nil || [UserModel sharedInstanced].userId == NULL || [[UserModel sharedInstanced].userId  isEqual: @""]) {
        return;
    }else{
        [self jspushRegisterToServers:registrationID];
    }
}
-(void) registerPushBieMing
{
    NSString *pushID = [UserModel sharedInstanced].mobile;
    [JPUSHService setAlias:pushID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
        NSLog(@"%@",iAlias);
        if (iResCode == 0) {
            NSLog(@"添加别名成功");
        }
    } seq:1];
    
    // iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送
    [UserModel sharedInstanced].jpushRegistrationID = pushID;
    [self postServersObjectName:pushID]; // 设备别名给后台 判断是否发送极光自定义消息  如果发送代表其他设备登录  提出其他设备登录状态
    
//    NSString *pushID = [UserModel sharedInstanced].jpushRegistrationID;
//    if (pushID == nil || pushID == NULL || pushID.length <= 0) {
//        NSString *deviceUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//        // 设置极光推送的标签和别名（标签是一个集合可以包含多个关键词 比如：红的、黄的、绿的）（别名是每个用户唯一的名称可以 根据这个别名给唯一的用户发推送）
//        //            NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"HelpYou_1.0",nil, nil]; // 标签
//        //    NSString *registrationIdStr = [UserModel sharedInstanced].jpushRegistrationID; // 别名
//        //            [JPUSHService setTags:set alias:deviceUUID callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
//        //登陆的时候加上别名
//        // 向极光发送设备别名
//        deviceUUID = [[NSUUID UUID] UUIDString];
//        deviceUUID=[deviceUUID stringByReplacingOccurrencesOfString:@"-"withString:@"_"];
//
//        [JPUSHService setAlias:deviceUUID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
//            NSLog(@"%@",iAlias);
//            if (iResCode == 0) {
//                NSLog(@"添加别名成功");
//            }
//        } seq:1];
//
//        // iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送
//        [UserModel sharedInstanced].jpushRegistrationID = deviceUUID;
//        [self postServersObjectName:deviceUUID]; // 设备别名给后台 判断是否发送极光自定义消息  如果发送代表其他设备登录  提出其他设备登录状态
//    }else{
//        [self postServersObjectName:pushID]; // 验证是否 提出其他设备登录账号
//    }
}
@end
